#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

from common.cleaner import clear
from oracle.common.device_manager.constants import StorageType, StorageVersion
from oracle.common.device_manager.device_info import DeviceInfo
from oracle.common.device_manager.storage_common import StorageCommon


class DeviceManager:
    @staticmethod
    def create_device_inst(info: DeviceInfo, storage_type: StorageType,
                           storage_version=StorageVersion.STORAGE_V6):
        if storage_type == StorageType.DORADO:
            device = StorageCommon(info)
            return device
        # 初始化失败，直接清理密码内存
        clear(info.password)
        return None

    @staticmethod
    def create_device_inst_by_ip_list(info: DeviceInfo, storage_type: StorageType,
                                      storage_version=StorageVersion.STORAGE_V6):
        device = None
        if storage_type == StorageType.DORADO:
            ips = info.ip.split(',')
            for ip in ips:
                info.ip = ip
                device = StorageCommon(info)
                if device.init_success:
                    return device
        # 初始化失败，直接清理密码内存
        clear(info.password)
        return device
